#include "lvgl/lvgl.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <linux/input.h>

// LVGL对象定义
lv_obj_t *main_screen;
lv_obj_t *time_label;
lv_obj_t *notice_label;
lv_obj_t *ad_img;
lv_obj_t *weather_screen;
lv_obj_t *notice_screen;
lv_obj_t *calendar_screen;
lv_obj_t *game_screen;

// 全局状态变量
volatile int run_chinese = 0;
#define NUM_PICS 5
int current_pic_index = 0;
lv_timer_t *ad_timer;
lv_timer_t *time_timer;
lv_timer_t *notice_timer;
lv_img_dsc_t ad_images[NUM_PICS];

// 图片路径
const char *icon_paths[NUM_PICS] = {
    "/picture/notice.bmp",
    "/picture/wheater.bmp",
    "/picture/game.bmp",
    "/picture/calendar.bmp",
    "/picture/ad.bmp"
};

// 功能函数声明
int preload_images(void);
void free_images(void);
void show_calendar(lv_event_t *e);
void show_weather(lv_event_t *e);
void show_notice(lv_event_t *e);
void start_game(lv_event_t *e);
void return_to_main(lv_event_t *e);
void update_time(lv_timer_t *timer);
void update_ad(lv_timer_t *timer);
void update_notice(lv_timer_t *timer);
void init_main_screen(void);

// 图片预加载
int preload_images() {
    for(int i = 0; i < NUM_PICS; i++) {
        lv_img_dsc_t *img = &ad_images[i];
        if(lv_img_buf_from_file(icon_paths[i], img) != LV_RES_OK) {
            printf("预加载图片失败: %s\n", icon_paths[i]);
            return -1;
        }
    }
    return 0;
}

// 释放图片资源
void free_images() {
    for(int i = 0; i < NUM_PICS; i++) {
        if(ad_images[i].data) {
            lv_img_buf_free(&ad_images[i]);
        }
    }
}

// 主界面初始化
void init_main_screen(void) {
    main_screen = lv_scr_act();
    lv_obj_set_style_bg_color(main_screen, lv_color_white(), 0);

    // 创建功能按钮
    lv_obj_t *notice_btn = lv_btn_create(main_screen);
    lv_obj_set_size(notice_btn, 100, 100);
    lv_obj_set_pos(notice_btn, 10, 20);
    lv_obj_t *notice_img = lv_img_create(notice_btn);
    lv_img_set_src(notice_img, &ad_images[0]);
    lv_obj_add_event_cb(notice_btn, show_notice, LV_EVENT_CLICKED, NULL);

    lv_obj_t *weather_btn = lv_btn_create(main_screen);
    lv_obj_set_size(weather_btn, 100, 100);
    lv_obj_set_pos(weather_btn, 10, 260);
    lv_obj_t *weather_img = lv_img_create(weather_btn);
    lv_img_set_src(weather_img, &ad_images[1]);
    lv_obj_add_event_cb(weather_btn, show_weather, LV_EVENT_CLICKED, NULL);

    lv_obj_t *calendar_btn = lv_btn_create(main_screen);
    lv_obj_set_size(calendar_btn, 100, 100);
    lv_obj_set_pos(calendar_btn, 10, 380);
    lv_obj_t *calendar_img = lv_img_create(calendar_btn);
    lv_img_set_src(calendar_img, &ad_images[3]);
    lv_obj_add_event_cb(calendar_btn, show_calendar, LV_EVENT_CLICKED, NULL);

    lv_obj_t *game_btn = lv_btn_create(main_screen);
    lv_obj_set_size(game_btn, 100, 100);
    lv_obj_set_pos(game_btn, 10, 140);
    lv_obj_t *game_img = lv_img_create(game_btn);
    lv_img_set_src(game_img, &ad_images[2]);
    lv_obj_add_event_cb(game_btn, start_game, LV_EVENT_CLICKED, NULL);

    // 创建时间标签
    time_label = lv_label_create(main_screen);
    lv_obj_set_style_text_font(time_label, &lv_font_montserrat_28, 0);
    lv_obj_set_style_text_color(time_label, lv_color_hex(0xff0000), 0);
    lv_obj_set_pos(time_label, 100, 0);
    lv_label_set_text(time_label, "");

    // 创建公告标签
    notice_label = lv_label_create(main_screen);
    lv_obj_set_style_text_font(notice_label, &lv_font_montserrat_28, 0);
    lv_obj_set_style_text_color(notice_label, lv_color_hex(0xff0000), 0);
    lv_obj_set_pos(notice_label, 100, 440);
    lv_label_set_text(notice_label, "小区公告加载中...");

    // 创建广告区域
    ad_img = lv_img_create(main_screen);
    lv_obj_set_pos(ad_img, 100, 50);
    lv_img_set_src(ad_img, &ad_images[4]);

    // 创建定时器
    time_timer = lv_timer_create(update_time, 1000, NULL);
    ad_timer = lv_timer_create(update_ad, 6000, NULL);
    notice_timer = lv_timer_create(update_notice, 3000, NULL);
}

// 更新时间显示
void update_time(lv_timer_t *timer) {
    time_t now = time(NULL);
    struct tm *sys_time = localtime(&now);
    char buf[64];
    
    sprintf(buf, "%04d年%02d月%02d日 %02d时%02d分%02d秒 天气：晴", 
            sys_time->tm_year + 1900, sys_time->tm_mon + 1, 
            sys_time->tm_mday, sys_time->tm_hour, 
            sys_time->tm_min, sys_time->tm_sec);
    
    lv_label_set_text(time_label, buf);
}

// 更新广告
void update_ad(lv_timer_t *timer) {
    current_pic_index = (current_pic_index + 1) % NUM_PICS;
    lv_img_set_src(ad_img, &ad_images[current_pic_index]);
}

// 更新公告
void update_notice(lv_timer_t *timer) {
    static int notice_index = 0;
    static char notices[20][256] = {{0}}; 
    static int notice_count = 0;
    static bool initialized = false;

    // 第一次运行时读取文件并初始化公告数组
    if (!initialized) {
        FILE *file = fopen("notice.txt", "r");
        if (file) {
            char buffer[1024];
            if (fgets(buffer, sizeof(buffer), file)) {
                char *token = strtok(buffer, "n"); // 使用'n'作为分隔符
                while (token != NULL && notice_count < 10) {
                    // 去除可能的换行符
                    token[strcspn(token, "\r\n")] = '\0';
                    
                    // 复制公告内容
                    strncpy(notices[notice_count], token, sizeof(notices[0]) - 1);
                    notice_count++;
                    
                    token = strtok(NULL, "n");
                }
            }
            fclose(file);
        } else {
            // 文件不存在时使用默认公告
            strcpy(notices[0], "无公告文件");
            notice_count = 1;
        }
        initialized = true;
    }

    // 显示公告（确保有公告可显示）
    if (notice_count > 0) {
        lv_label_set_text(notice_label, notices[notice_index]);
        notice_index = (notice_index + 1) % notice_count;
    }
}

// 显示天气界面
void show_weather(lv_event_t *e) {
    weather_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(weather_screen, lv_color_white(), 0);
    
    lv_obj_t *bg = lv_img_create(weather_screen);
    lv_img_set_src(bg, "/picture/big_wheater.bmp");
    
    lv_obj_t *return_btn = lv_btn_create(weather_screen);
    lv_obj_set_size(return_btn, 80, 40);
    lv_obj_set_pos(return_btn, 700, 10);
    lv_obj_t *label = lv_label_create(return_btn);
    lv_label_set_text(label, "返回");
    lv_obj_add_event_cb(return_btn, return_to_main, LV_EVENT_CLICKED, NULL);
    
    // 显示古诗
    lv_obj_t *poem_title = lv_label_create(weather_screen);
    lv_obj_set_style_text_font(poem_title, &lv_font_montserrat_28, 0);
    lv_obj_set_style_text_color(poem_title, lv_color_black(), 0);
    lv_obj_set_pos(poem_title, 45, 5);
    lv_label_set_text(poem_title, "初夏即事");
    
    lv_obj_t *poem_author = lv_label_create(weather_screen);
    lv_obj_set_style_text_font(poem_author, &lv_font_montserrat_28, 0);
    lv_obj_set_style_text_color(poem_author, lv_color_black(), 0);
    lv_obj_set_pos(poem_author, 20, 40);
    lv_label_set_text(poem_author, "北宋 王安石");
    
    lv_scr_load(weather_screen);
}

// 显示公告界面
void show_notice(lv_event_t *e) {
    notice_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(notice_screen, lv_color_white(), 0);
    
    lv_obj_t *bg = lv_img_create(notice_screen);
    lv_img_set_src(bg, "/picture/big_notice.bmp");
    
    lv_obj_t *return_btn = lv_btn_create(notice_screen);
    lv_obj_set_size(return_btn, 80, 40);
    lv_obj_set_pos(return_btn, 700, 10);
    lv_obj_t *label = lv_label_create(return_btn);
    lv_label_set_text(label, "返回");
    lv_obj_add_event_cb(return_btn, return_to_main, LV_EVENT_CLICKED, NULL);
    
    lv_scr_load(notice_screen);
}

// 显示日历界面
void show_calendar(lv_event_t *e) {
    calendar_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(calendar_screen, lv_color_white(), 0);
    
    lv_obj_t *calendar = lv_calendar_create(calendar_screen);
    lv_obj_set_size(calendar, 600, 400);
    lv_obj_center(calendar);
    
    lv_obj_t *return_btn = lv_btn_create(calendar_screen);
    lv_obj_set_size(return_btn, 80, 40);
    lv_obj_set_pos(return_btn, 700, 10);
    lv_obj_t *label = lv_label_create(return_btn);
    lv_label_set_text(label, "返回");
    lv_obj_add_event_cb(return_btn, return_to_main, LV_EVENT_CLICKED, NULL);
    
    lv_scr_load(calendar_screen);
}

// 启动游戏
void start_game(lv_event_t *e) {
    game_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(game_screen, lv_color_white(), 0);
    
    // 2048游戏界面
    lv_obj_t *game_bg = lv_img_create(game_screen);
    lv_img_set_src(game_bg, "/picture/2048.bmp");
    
    lv_obj_t *return_btn = lv_btn_create(game_screen);
    lv_obj_set_size(return_btn, 80, 40);
    lv_obj_set_pos(return_btn, 700, 10);
    lv_obj_t *label = lv_label_create(return_btn);
    lv_label_set_text(label, "返回");
    lv_obj_add_event_cb(return_btn, return_to_main, LV_EVENT_CLICKED, NULL);
    
    lv_scr_load(game_screen);
}

// 返回主界面
void return_to_main(lv_event_t *e) {
    lv_scr_load(main_screen);
}
